package com.mzj.saas.hidden;

import com.haixiaoke.saas.repository.core.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

import static com.haixiaoke.saas.repository.core.AjaxResult.success;

/**
 * @author qixi
 * @date 2024/6/13
 */
@RestController
@RequestMapping("/hidden")
public class HiddenController {

    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    private final String HIDDEN_STATE = "sys.hidden.state";


    @PostConstruct()
    public void init() {
        redisTemplate.opsForValue().set(HIDDEN_STATE, "0");
    }


    @GetMapping
    private AjaxResult hidden() {
        String hiddenState = redisTemplate.opsForValue().get(HIDDEN_STATE);
        if (hiddenState != null) {
            return AjaxResult.success("操作成功", Integer.parseInt(hiddenState));
        }
        return success();
    }


    @PostMapping
    private AjaxResult setHidden(@RequestParam("state") String state) {
        redisTemplate.opsForValue().set(HIDDEN_STATE, state);
        return success();
    }
}
